package cn.lcfms.app.admin.controller;


import java.util.HashMap;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.lcfms.app.admin.bean.AdminBean;
import cn.lcfms.bin.BaseCache;
import cn.lcfms.bin.Userinfo;
import cn.lcfms.bin.annotation.PermitPoll;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.utils.CommonUtils;


@PermitPoll(type="系统功能",name="后台框架")
@Controller("admin.IndexController")
@RequestMapping("/admin/index")
public class IndexController extends AdminBaseController{	
	
	@RequestMapping("/ace")
	public ModelAndView ace(HttpServletRequest request,@RequestParam(defaultValue="0",required=false) int firstitemid){
		if(firstitemid==0) {
			firstitemid=Application.config("ace.firstitemid");
		}	
		Userinfo.put("firstitemid", firstitemid);
		ModelAndView view=new ModelAndView("admin/ace");	
		return view;
	}
	//fa-object-group
	
	@RequestMapping("/first")
	public ModelAndView first(HttpServletRequest request){
		ModelAndView view=new ModelAndView("admin/first");					
		return view;
	}	
	
	@RequestMapping("/clearCache")
	@ResponseBody
	public String clearCache(){
		BaseCache.clear();
		return "1";
	}	
	
	@RequestMapping("/userinfo")
	public ModelAndView userinfo(HttpServletRequest request){
		BaseService service = Application.getService("role");
		List<HashMap<String, Object>> list = service.selectList();
		ModelAndView view=new ModelAndView("admin/frame/userinfo","list",list);
		int aid=Userinfo.get("aid");
		AdminBean bean=service.setTable("admin").selectOne(AdminBean.class, aid);
		view.addObject("user", bean);
		return view;
	}	
	
	/*
	 * 处理更新个人信息
	 */
	@RequestMapping("/setUser")
	public String setUser(String password,AdminBean admin) throws Exception{		 
		BaseService service = Application.getService("admin");
		String aname=Userinfo.get("aname");
		if(!aname.equals(admin.getAname())) {
			int count = service.where("aname=#{aname}").setData(admin.getAname()).selectCount();
			if(count>0) {
				return "{code:0,msg:登陆名重复！}";   
			}
		}
		if(password.length()==32){
			service.update(admin,"password,did,rid,status");
		}else{	
		    password=CommonUtils.md5(CommonUtils.md5(admin.getAname()+password));
		    admin.setPassword(password);
		    service.update(admin,"did,rid,status");
		}	
		return "{code:1,msg:修改成功！}";   
	}
	
	@RequestMapping("test")
	@ResponseBody
	public String test(HttpServletRequest request) {
		return "1111";
	}	
}
